package Classes.GameUtils// ActionScript file
{
	import flash.display.Graphics;

	public class GU_Vector2D
	{
		private var _x:Number;
		private var _y:Number;
		public function GU_Vector2D(x:Number=0,y:Number=0):void
		{
			this._x=x;
			this._y=y;
		}
		
		public static function creatByLenAndDegree(len:Number,degree:Number):GU_Vector2D
		{
			var vx:Number=len*Math.cos(degree*Math.PI/180);
			var vy:Number=len*Math.sin(degree*Math.PI/180);
			return new GU_Vector2D(vx,vy);
		}
		
		public function draw(graphics:Graphics,color:uint=0):void
		{
			graphics.lineStyle(0,color);
			graphics.moveTo(0,0);
			graphics.lineTo(this.x,this.y);
		}
		
		public function clone():GU_Vector2D
		{
			return new GU_Vector2D(this.x,this.y);
		}
		
		public function zero():GU_Vector2D
		{
			this.x=0;
			this.y=0;
			return this;
		}
		
		public function isZero():Boolean
		{
			return this.x==0 && this.y==0;
		}
		
		public function normalize():GU_Vector2D
		{
			if(!this.length)
			{
				this.x=0;
				return this;
			}
			var len:Number=this.length;
			this.x/=len;
			this.y/=len;
			return this;
		}
		
		public function truncate(max:Number):GU_Vector2D
		{
			this.length=Math.min(max,this.length);
			return this;
		}
		
		public function reverse():GU_Vector2D
		{
			this.x=-this.x;
			this.y=-this.y;
			return this;
		}
		
		public function isNormalized():Boolean
		{
			return this.length==1.0;
		}
		
		public function dotProd(v2:GU_Vector2D):Number
		{
			return this.x*v2.x+this.y*v2.y;
		}
		
		public static function angelBetween(v1:GU_Vector2D,v2:GU_Vector2D):Number
		{
			if(!v1.isNormalized()) v1=v1.clone().normalize();
			if(!v2.isNormalized()) v2=v2.clone().normalize();
			return Math.acos(v1.dotProd(v2));
		}
		
		public function sign(v2:GU_Vector2D):int
		{
			return this.perp.dotProd(v2)<0?-1:1;
		}
		
		public function dist(v2:GU_Vector2D):Number
		{
			return Math.sqrt(this.distSQ(v2));
		}
		
		public function distSQ(v2:GU_Vector2D):Number
		{
			var dx:Number=v2.x-this.x;
			var dy:Number=v2.y-this.y;
			return dx*dx+dy*dy;
		}
		
		public function add(v2:GU_Vector2D):GU_Vector2D
		{
			return new GU_Vector2D(this.x+v2.x,this.y+v2.y);
		}
		
		public function subtract(v2:GU_Vector2D):GU_Vector2D
		{
			return new GU_Vector2D(this.x-v2.x,this.y-v2.y);
		}
		
		public function multiply(value:Number):GU_Vector2D
		{
			return new GU_Vector2D(this.x*value,this.y*value);
		}
		
		public function divide(value:Number):GU_Vector2D
		{
			return new GU_Vector2D(this.x/value,this.y/value);
		}
		
		public function equals(v2:GU_Vector2D):Boolean
		{
			return this.x==v2.x && this.y==v2.y;
		}
		
		public function toString():String
		{
			return "[2D向量(x:"+this.x+",y:"+this.y+")]";
		}
		
		/*getter & setter*/
		public function get x():Number
		{
			return _x;
		}

		public function set x(value:Number):void
		{
			_x = value;
		}

		public function get y():Number
		{
			return _y;
		}

		public function set y(value:Number):void
		{
			_y = value;
		}
		
		public function set length(value:Number):void
		{
			var a:Number=this.angel;
			this.x=Math.cos(a)*value;
			this.y=Math.sin(a)*value;
		}
		
		public function get length():Number
		{
			return Math.sqrt(this.lengthSQ);
		}
		
		public function get lengthSQ():Number
		{
			return this.x*this.x+this.y*this.y;
		}
		
		public function set angel(value:Number):void
		{
			var len:Number=this.length;
			this.x=Math.cos(value)*len;
			this.y=Math.sin(value)*len;
		}
		
		public function get angel():Number
		{
			return Math.atan2(this.y,this.x);
		}
		
		public function get perp():GU_Vector2D
		{
			return new GU_Vector2D(-this.y,this.x);
		}

	}
}